package com.gupao.proxy.dbroute.proxy;

import com.gupao.proxy.dbroute.Order;
import com.gupao.proxy.dbroute.OrderService;
import com.gupao.proxy.dbroute.db.DynamicDataSourceEntity;

import java.text.SimpleDateFormat;
import java.util.Date;

public class StaticOrderServiceProxy {

    private SimpleDateFormat yearFormat = new SimpleDateFormat("yyyy");

    private OrderService orderService;

    public StaticOrderServiceProxy(OrderService orderService) {
        this.orderService = orderService;
    }

    public void createOrder(Order order){
        Long time = order.getCreateTime();
        Integer dbRouter = Integer.valueOf(yearFormat.format(new Date(time)));
        DynamicDataSourceEntity.set(dbRouter);
        this.orderService.createOrder(order);
        DynamicDataSourceEntity.restore();
    }
}
